package cn.gene.timeyan.security.sso.controller;

import cn.gene.timeyan.common.restful.JsonResult;
import cn.gene.timeyan.common.restful.ResponseCode;
import cn.gene.timeyan.pojo.valid.security.dto.validateCodeDTO;
import cn.gene.timeyan.security.sso.pojo.dto.UserLoginDTO;
import cn.gene.timeyan.security.sso.pojo.vo.TokenVO;
import cn.gene.timeyan.security.sso.security.service.user.IUserSSOService;
import cn.gene.timeyan.security.sso.service.IUserInfoService;
import cn.gene.timeyan.security.sso.utils.LoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 实现前台用户登录的请求和登出请求*
 *
 * @author LWG
 * @date 2022/7/22 15:06
 * @since 1.0.0@gene.cn
 */
@RestController
@RequestMapping("/user/sso")
@Api(tags = "前台管理用户认证")
@Slf4j
public class UserSSOController {

    @Autowired
    IUserInfoService userInfoService;

    @Autowired
    private IUserSSOService userSSOService;

    @Value("${jwt.tokenHead}")
    private String jwtTokenHead;
    @ApiOperation(value = "前台单点登录认证登录")
    @PostMapping("/login")
    public JsonResult<TokenVO> doLogin(@Valid UserLoginDTO userLoginDTO, HttpServletRequest request){
        //先补充数据
        String ip = LoginUtils.getIpAddress(request);
        log.info("远程ip地址:{}",ip);
        String userAgent=request.getHeader("User-Agent");
        log.info("远程客户端:{}",userAgent);
        userLoginDTO.setIp(ip);
        userLoginDTO.setUserAgent(userAgent);
        String token = userSSOService.doLogin(userLoginDTO);
        TokenVO tokenVO = new TokenVO();
        tokenVO.setTokenHeader(jwtTokenHead);
        tokenVO.setTokenValue(token);
        return JsonResult.ok(tokenVO);
    }

    /**
     * 通过验证码登录
     *
     * @return
     */
    @ApiOperation(value = "验证码登录", notes = "通过调用loginByValidCode方法，使用手机号或者邮箱配合验证码进行登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginKey", value = "用户登录的手机号或邮箱", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", required = true),
            @ApiImplicitParam(name = "loginByValidCodeDTO", value = "以上属性被封装到这个DTO里")
    })
    @PostMapping("/loginByValidCode")
    JsonResult loginByValidCode(@Validated @RequestBody validateCodeDTO validateCodeDTO, HttpServletRequest request) {
        request.getSession().setAttribute("user", userInfoService.loginByValidCode(validateCodeDTO));
        return JsonResult.ok();
    }

    /**
     * <p>登出logout</p>
     * <p>没有任何实际业务逻辑</p>
     */
    @ApiOperation(value = "前台单点登录认证登出")
    @PostMapping("/logout")
    public JsonResult doLogout(@RequestHeader(name = "Authorization") String token){
        userSSOService.doLogout(token);
        return JsonResult.ok();
    }

}

